package org.jim.server.helper.db.mybatis;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.jim.server.helper.db.mybatis.dao.UserMapper;
import org.jim.server.helper.db.mybatis.model.User;
import org.jim.server.helper.db.mybatis.dao.GroupMessageMapper;
import org.jim.server.helper.db.mybatis.model.GroupMessage;
import org.jim.server.helper.db.mybatis.dao.UserMessageMapper;
import org.jim.server.helper.db.mybatis.model.UserMessage;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestMyBatis {
	static SqlSessionFactory sqlSessionFactory = null;
	static {
		sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();
	}

	public static void main(String[] args) {
	//	GroupMessageAdd();
//		UserMessageAdd();
//		testAdd();
//		UserAdd();
//		Map map = new HashMap();
//		map.put("from",1);
//		map.put("groupId",101);
//		map.put("beginTime",0);
//		map.put("endTime",0);
//		getGroupMessage(map);
		getUserMessageByRedisId("e9284cdebd1a4752ae853c4a638ec7f4");
	}

//	public static void UserAdd() {
//		SqlSession sqlSession = sqlSessionFactory.openSession();
//		try {
//			UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//			User user = new User(new Long(153447023),"1234","1234","1234","1234");
//			userMapper.insertUser(user);
//			sqlSession.commit();// 这里一定要提交，不然数据进不去数据库中
//		} finally {
//			sqlSession.close();
//		}
//	}
//	public static void GroupMessageAdd() {
//		SqlSession sqlSession = sqlSessionFactory.openSession();
//		try {
//			GroupMessageMapper groupMessageMapper = sqlSession.getMapper(GroupMessageMapper.class);
//			GroupMessage groupMessage = new GroupMessage("1234",new Integer(1234),new Integer(1234),"1234",new Long(1534474507023),new Long(1234),new Long(1234),new Integer(1234));
//			groupMessageMapper.insertGroupMessage(groupMessage);
//			sqlSession.commit();// 这里一定要提交，不然数据进不去数据库中
//		} finally {
//			sqlSession.close();
//		}
//	}
//
//	public static void UserMessageAdd() {
//		SqlSession sqlSession = sqlSessionFactory.openSession();
//		try {
//			UserMessageMapper userMessageMapper = sqlSession.getMapper(UserMessageMapper.class);
//			UserMessage userMessage = new UserMessage("1234",new Integer(1234),new Integer(1234),"1234",new Long(1234),new Long(1234),new Long(1234),new Integer(1234));
//			userMessageMapper.insertUserMessage(userMessage);
//			sqlSession.commit();// 这里一定要提交，不然数据进不去数据库中
//		} finally {
//			sqlSession.close();
//		}
//	}

//	public static void testAdd() {
//		SqlSession sqlSession = sqlSessionFactory.openSession();
//		try {
//			UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//			User user = new User(new Integer(2),"lisi");
//			userMapper.insertUser(user);
//			sqlSession.commit();// 这里一定要提交，不然数据进不去数据库中
//		} finally {
//			sqlSession.close();
//		}
//	}

//	public static void getUserMessage(Map map) {
//		SqlSession sqlSession = sqlSessionFactory.openSession();
//		try {
//			UserMessageMapper userMessageMapper = sqlSession.getMapper(UserMessageMapper.class);
//			List<UserMessage> userMessage = userMessageMapper.getUserMessage(map);
//			System.out.println("id: " + userMessage.get(0).getId() + "|getContent: "
//					+ userMessage.get(0).getContent() + "|getCreateTime: "
//					+ userMessage.get(0).getCreateTime()+ "|chatType: "
//							+ userMessage.get(0).getChatType()
//					);
//		} finally {
//			sqlSession.close();
//		}
//	}
//		public static void getGroupMessage(Map map) {
//		SqlSession sqlSession = sqlSessionFactory.openSession();
//		try {
//			GroupMessageMapper groupMessageMapper = sqlSession.getMapper(GroupMessageMapper.class);
//			List<GroupMessage> groupMessage = groupMessageMapper.getGroupMessage(map);
//			System.out.println("id: " + groupMessage.get(0).getId() + "|getContent: "
//					+ groupMessage.get(0).getContent() + "|getCreateTime: "
//					+ groupMessage.get(0).getCreateTime()+ "|chatType: "
//							+ groupMessage.get(0).getChatType()
//					);
//		} finally {
//			sqlSession.close();
//		}
//	}
		public static void getUserMessageByRedisId(String redisId) {
		SqlSession sqlSession = sqlSessionFactory.openSession();
		try {
			UserMessageMapper userMessageMapper = sqlSession.getMapper(UserMessageMapper.class);
			List<UserMessage> userMessage = userMessageMapper.getUserMessageByRedisId(redisId);
			System.out.println("id: " + userMessage.get(0).getId() + "|getContent: "
					+ userMessage.get(0).getContent() + "|getCreateTime: "
					+ userMessage.get(0).getCreateTime()+ "|chatType: "
							+ userMessage.get(0).getChatType()
					);
		} finally {
			sqlSession.close();
		}
	}
}
